<template>
    <nav class="navbar navbar-dark bg-primary justify-content-between mb-4 px-4">
        <a class="navbar-brand" href="/">Gorit 的知乎专栏</a>
        <ul v-if="!user.isLogin" class=" list-inline mb-0">
            <li class="list-inline-item">
                <a @click.prevent="handleLogin" href="" class="btn btn-outline-light my-2">登录</a>
            </li>
            <li class="list-inline-item">
                <a @click.prevent="handleSignUp" href="" class="btn btn-outline-light my-2">注册</a>
            </li>
        </ul>
        <ul v-else class=" list-inline mb-0 ">
            <li class="list-inline-item">
                <Dropdown :title="`你好,` + user.nickName">
                    <dropdown-item><a href="#" @click.prevent="handleNewPost" class="dropdown-item">新建文章</a></dropdown-item>
                    <dropdown-item><router-link :to="`/column/${user.column}`" class="dropdown-item">我的专栏</router-link></dropdown-item>
                    <dropdown-item disabled><a href="#" class="dropdown-item">编辑资料</a></dropdown-item>
                    <dropdown-item><a href="#" @click.prevent="handleLogout" class="dropdown-item">退出登录</a></dropdown-item>
                </Dropdown>
                <!-- <a href="" class="btn btn-outline-light my-2">你好,{{user.name}}</a> -->
            </li>
        </ul>
    </nav>
</template>

<script lang="ts" setup>
import { PropType } from "@vue/runtime-core";
import Dropdown from "./Dropdown.vue";
import DropdownItem from "./DropdownItem.vue";
import { useRouter } from "vue-router";
import { GlobalDataProps, UserProps } from "@/types";
import { useStore } from 'vuex';
import createMessage from "./createMessage";

const store = useStore<GlobalDataProps>();

const props = defineProps({
    user: {
        type: Object as PropType<UserProps>,
        required: true
    }
})

const router = useRouter();


const handleLogin = () => {
    router.push("/login");
}

const handleSignUp = () => {
    router.push({name: 'Signup'});
}
const handleNewPost = () => {
    router.push('/create');
}

const handleLogout = () => {
    // 清除 token 之类的
    store.commit('logout');
    createMessage('将在 2s 内退出登录','default');
    setTimeout(() => {
        router.replace("/logout");
    });
    // props.user.isLogin = false;
}



</script>

<style scoped>

</style>
